package cn.exrick.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.mapper.TbMemberMapper;
import cn.exrick.mapper.TbResourceCatMapper;
import cn.exrick.mapper.TbResourceMapper;
import cn.exrick.pojo.TbMember;
import cn.exrick.pojo.TbResource;
import cn.exrick.pojo.TbResourceCat;
import cn.exrick.pojo.TbResourceExample;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.front.ResourceResult;
import cn.exrick.service.ResourceService;
import cn.exrick.utils.mybatis.Page;
import cn.exrick.utils.mybatis.PageParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private TbResourceMapper tbResourceMapper;
    @Autowired
    private TbResourceCatMapper tbResourceCatMapper;
    @Autowired
    private TbMemberMapper tbMemberMapper;

    @Override
    public DataTablesResult getResourcePage(int draw, int start, int length, String search, Long catId,
                                            String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result = new DataTablesResult();

        //分页执行查询返回结果
        PageHelper.startPage(start / length + 1, length);
        PageHelper.orderBy(orderCol + " " + orderDir);
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("search", search);
        if(null != catId){
            paramsMap.put("catId", catId);
        }
        paramsMap.put("minDate", minDate);
        paramsMap.put("maxDate", maxDate);

        List<TbResource> list = tbResourceMapper.selectByPage(paramsMap);
        PageInfo<TbResource> pageInfo = new PageInfo<>(list);
        result.setRecordsFiltered((int) pageInfo.getTotal());
        result.setRecordsTotal((int) pageInfo.getTotal());

        result.setDraw(draw);
        result.setData(list);

        return result;
    }

    @Override
    public int save(TbResource tbResource) {
        TbResourceCat tbResourceCat = tbResourceCatMapper.selectByPrimaryKey(tbResource.getCatId());
        if(null == tbResourceCat){
            throw new XmallException("资源分类不存在");
        }
        int result = tbResourceMapper.insertSelective(tbResource);
        if(1 != result){
            throw new XmallException("提交问答失败");
        }
        return result;
    }

    @Override
    public int updateStatus(Long id) {
        TbResource tbResource = tbResourceMapper.selectByPrimaryKey(id);
        if(null == tbResource){
            throw new XmallException("资源不存在");
        }
        if(tbResource.getStatus() == 0){
            tbResource.setStatus(1);
        } else {
            tbResource.setStatus(0);
        }
        int result = tbResourceMapper.updateByPrimaryKeySelective(tbResource);
        if(1 != result){
            throw new XmallException("显示/隐藏失败");
        }
        return 1;
    }

    @Override
    public Page<ResourceResult> getResourcePageForApi(int currentPage, int size, Long category, String search) {
        // 分页
        if (currentPage <= 0) {
            currentPage = 1;
        }
        PageHelper.startPage(currentPage, size);
        PageHelper.orderBy("created desc");
        PageParam pageParam = new PageParam(currentPage - 1, size);
        Page<ResourceResult> page = new Page<ResourceResult>(pageParam);
        List<ResourceResult> list = new ArrayList<>();

        TbResourceExample example = new TbResourceExample();
        TbResourceExample.Criteria criteria = example.createCriteria();
        // 查询显示状态的记录
        criteria.andStatusEqualTo(0);
        if(null != category){
            criteria.andCatIdEqualTo(category);
        }
        if(StringUtils.isNotBlank(search)){
            criteria.andContentLike("%" + search + "%");
        }

        List<TbResource> tbResources = tbResourceMapper.selectByExample(example);

        ResourceResult result;
        for (TbResource tbResource : tbResources){
            result = getResourceResult(tbResource);
            list.add(result);
        }

        PageInfo<TbResource> pageInfo = new PageInfo<>(tbResources);
        page.setTotalCount((int) pageInfo.getTotal());
        page.setResult(list);

        return page;
    }

    @Override
    public ResourceResult getDetailForApi(Long resourceId) {
        TbResource tbResource = tbResourceMapper.selectByPrimaryKey(resourceId);
        if(null != tbResource){
            return getResourceResult(tbResource);
        }
        return null;
    }

    private ResourceResult getResourceResult(TbResource tbResource){
        ResourceResult result = new ResourceResult();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        result.setResourceId(tbResource.getId());
        result.setContent(tbResource.getContent());
        result.setImage(tbResource.getImage());
        result.setCreateDate(formatter.format(tbResource.getCreated()));
        result.setUrl("/dist/index.html#/downLoad1/resourceId=" + tbResource.getId());

        TbMember tbMember = tbMemberMapper.selectByPrimaryKey(tbResource.getMemberId());
        if(null != tbMember){
            result.setMemberId(tbMember.getId());
            result.setMemberName(tbMember.getUsername());
            result.setMemberPhone(tbMember.getPhone());
            result.setMemberAvatar(tbMember.getFile());
            result.setMemberLocation(tbMember.getCity());
            result.setMemberCertify(tbMember.getCertify());
        }

        return result;
    }

}
